package com.zhf.service.impl;

import com.zhf.base.StatusCode;
import com.zhf.dao.CourseContentDao;
import com.zhf.dao.impl.CourseContentDaoImpl;
import com.zhf.pojo.Course;
import com.zhf.pojo.Course_Lesson;
import com.zhf.pojo.Course_Section;
import com.zhf.service.CourseContentService;
import com.zhf.utils.DateUtils;

import java.util.List;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2020/10/31 16:13
 */

/**
 * 课程内容管理Service层实现类
 */
public class CourseContentServiceImpl implements CourseContentService {

    CourseContentDao contentDao = new CourseContentDaoImpl();

    @Override
    public List<Course_Section> findSectionAndLessonByCourseId(int courseId) {
        List<Course_Section> sections = contentDao.findSectionAndLessonByCourseId(courseId);
        return sections;
    }

    @Override
    public Course findCourseById(int courseId) {
        Course course = contentDao.findCourseByID(courseId);

        return course;
    }

    @Override
    public String saveSection(Course_Section section) {
        //补全章节信息
        //状态 0 隐藏   1 待更新   2已发布
        section.setStatus(2);
        String dateFormart = DateUtils.getDateFormart();
        section.setCreate_time(dateFormart);
        section.setUpdate_time(dateFormart);

        int row = contentDao.saveSection(section);

        if (row > 0) {
            //保存成功
            String result = StatusCode.SUCCESS.toString();
            return result;
        } else {
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }

    @Override
    public String updateSection(Course_Section section) {
        //补全信息
        section.setUpdate_time(DateUtils.getDateFormart());

        //调用Dao
        int i = contentDao.updateSection(section);
        if (i > 0) {
            String result = StatusCode.SUCCESS.toString();
            return result;
        } else {
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }

    @Override
    public String updateSectionStatus(int id, int status) {
        int row = contentDao.updateSectionStatus(id, status);
        if (row > 0) {
            String result = StatusCode.SUCCESS.toString();
            return result;
        } else {
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }

    @Override
    public String saveLesson(Course_Lesson lesson) {
        int row = contentDao.saveLesson(lesson);
        if (row > 0) {
            String result = StatusCode.SUCCESS.toString();
            return result;
        } else {
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }

    @Override
    public String updateLesson(Course_Lesson lesson) {
        int row = contentDao.updateLesson(lesson);
        if (row > 0) {
            String result = StatusCode.SUCCESS.toString();
            return result;
        } else {
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }
}
